import Vue from 'vue'
import VueRouter from 'vue-router'

// 导入登入组件
import Login from "@/components/login/login.vue"

// 导入后台组件
import Back from "@/components/Backstage.vue"
// 导入用户管理组件
import UserMain from "@/components/Tabs/userManage.vue"
// 导入权限组件
import Permiss from "@/components/Tabs/permission.vue"
// 导入商品模块
import Goods from "@/components/Tabs/goods.vue"
// 导入订单模块
import Order from "@/components/Tabs/order.vue"
// 导入系统模块
import Sys from "@/components/Tabs/system.vue"
import userInfo from "@/components/Tabs/userinfo.vue"

// 将router设为插件
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: "/login"},
    { path: '/login', component: Login },
    { 
      path: '/back',
      component: Back,
      redirect: '/back/userman',
      children: [
        {path: 'userman', component: UserMain},
        {path: 'permiss', component: Permiss},
        {path: 'goods', component: Goods},
        {path: 'order', component: Order},
        {path: 'sys', component: Sys},
        {path: 'userinfo/:id', component: userInfo, props: true},
      ]
    }
  ]
})

// 配置导航守卫
router.beforeEach(function(to, from, next) {
  if(to.path === '/back/userman'){
    const token = localStorage.getItem('token')
    if(token){
      next()
    }else{
      next('/login')
    }
  } else{
    next()
  }
})

export default router
